/*
 * Copyright (C) 2012 www.aboboo.com
 * 
 */
package com.aboboo.android.json;

import java.util.ArrayList;

import java.io.BufferedReader;
import java.io.InputStreamReader;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONObject;
import org.json.JSONArray;
import org.json.JSONException;

import com.aboboo.android.constant.AbobooConstant;

import com.aboboo.android.json.JsonCourseGroup;
import com.aboboo.android.json.JsonCourseware;

/**
 * 解析JSON文件，得到课件组或者课件的基础信息，用于在“课件资源”中展示
 * 
 * @author fangpeng
 * @version 1.0 2012-08-06
 * 
 */
public class ParseJsonInfo {

	/**
	 * 从服务器返回指定地址的JSON信息
	 * 
	 * @return
	 */
	private static String getJSONInfoFromServer(final String url) {
		StringBuilder builder = new StringBuilder();
		HttpClient client = new DefaultHttpClient();

		HttpGet httpget = new HttpGet(url);
		try {
			HttpResponse response = client.execute(httpget);
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					response.getEntity().getContent()));
			for (String s = reader.readLine(); s != null; s = reader.readLine()) {
				builder.append(s);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return builder.toString();
	}

	/**
	 * 解析JSON对象，得到课件组信息
	 * 
	 * @return
	 */
	public static ArrayList<JsonCourseGroup> getCourseGroups() {
		ArrayList<JsonCourseGroup> courseGroupList = new ArrayList<JsonCourseGroup>();
		try {
			JSONObject jsonObject = new JSONObject(
					getJSONInfoFromServer(AbobooConstant.LATEST_PACKAGES_URL));
			// String code = jsonObject.getString("code");
			JSONArray packagesJsonArray = jsonObject.getJSONArray("packages");
			int arrayLength = packagesJsonArray.length();
			for (int i = 0; i < arrayLength; i++) {
				JSONObject coursegroupObj = packagesJsonArray.getJSONObject(i);
				String description = coursegroupObj.getString("description");
				String url = coursegroupObj.getString("url");
				String modified = coursegroupObj.getString("modified");
				int members = coursegroupObj.getInt("members");
				int coursewares = coursegroupObj.getInt("coursewares");
				String img_url = coursegroupObj.getString("img_url");
				int id = coursegroupObj.getInt("id");
				String name = coursegroupObj.getString("name");

				JsonCourseGroup group = new JsonCourseGroup(description, url,
						modified, members, coursewares, img_url, id, name);
				courseGroupList.add(group);
			}

			// System.out.println("first course = " +
			// ((JsonCourseGroup)courseGroupList.get(1)).toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return courseGroupList;
	}

	/**
	 * 解析JSON对象，得到指定URL课件组下的课件信息
	 * 
	 * @return
	 */
	public static ArrayList<JsonCourseware> getCoursewares(String courseGroupUrl) {
		ArrayList<JsonCourseware> coursewareList = new ArrayList<JsonCourseware>();
		try {
			String jsonString = getJSONInfoFromServer(AbobooConstant.ABOBOO_URL
					+ courseGroupUrl);
			JSONObject courseGroupObj = new JSONObject(jsonString);
			JSONObject packageObj = courseGroupObj.getJSONObject("package");
			JSONArray coursewaresArray = packageObj.getJSONArray("coursewares");
			int arrayLength = coursewaresArray.length();
			for (int i = 0; i < arrayLength; i++) {
				JSONObject coursewareObj = coursewaresArray.getJSONObject(i);
				String modified = coursewareObj.getString("modified");
				String uhash = coursewareObj.getString("uhash");
				int id = coursewareObj.getInt("id");
				String name = coursewareObj.getString("name");
				String url = coursewareObj.getString("url");
				String img_url = coursewareObj.getString("img_url");
				String last_revision = "";
				if (!coursewareObj.isNull("last_revision")) {
					last_revision = coursewareObj.getString("last_revision");
				}
				String download_url = coursewareObj.getString("download_url");
				int lessonnum = coursewareObj.getInt("lessonnum");
				String class1 = coursewareObj.getString("class1");
				String class2 = coursewareObj.getString("class2");
				int filesize = coursewareObj.getInt("filesize");

				JsonCourseware courseware = new JsonCourseware(modified, uhash,
						id, name, url, img_url, last_revision, download_url,
						lessonnum, class1, class2, filesize);
				coursewareList.add(courseware);
			}
			//System.out.println(((JsonCourseware)coursewareList.get(0)).toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return coursewareList;
	}
}
